package com.lemt.sunmao.controller;


import com.lemt.sunmao.domain.User;
import com.lemt.sunmao.response.ResponseCode;
import com.lemt.sunmao.response.ResponstData;
import com.lemt.sunmao.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 服务生产者，供消费者调用
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页条件查询用户--此处查询需要先在es中保存用户信息
     * */
    @GetMapping("/findPageUsers")
    public ResponstData findPageUsers(String username, Integer currentPage, Integer pageSize){
        ResponstData responstData = userService.findPageUsers(username,currentPage,pageSize);
        return responstData;
    }

    /**
     * 根据id查询用户
     * */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet") //此方法失败调用下面备选--一般不会调用，因为基本情况都已实现
    public ResponstData getById(@PathVariable("id") Long id){
        ResponstData responstData = userService.getById(id);
        return responstData;
    }

    // 备选
    public ResponstData hystrixGet(@PathVariable("id") Long id){
        String msg = "没有对应id=>" + id + "信息";
        return new ResponstData(ResponseCode.FAIL,msg);
    }

    /**
     *  根据id删除用户
     * */
    @DeleteMapping("/{id}")
    public ResponstData deleteById(@PathVariable("id") Long id){
        ResponstData responstData = userService.deleteById(id);
        return responstData;
    }

    /**
     * 注册用户
     * */
    @PostMapping
    public ResponstData save(@RequestBody User user){
        ResponstData responstData = userService.insert(user);
        return responstData;
    }
}
